<?php

namespace App\Models\Hotel;

use App\Models\Area;
use App\Models\Label;
use Carbon\Carbon;
use App\Models\BaseModel;
use App\Helpers\MoneyHelper;
use App\Format\BaseFormat;

/**
 * App\Models\Hotel\Hotel
 *
 * @property int $id
 * @property string $name 名称
 * @property string $thumbnail 封面
 * @property int $province_id 省ID
 * @property string $province_text 省文本
 * @property int $city_id 市ID
 * @property string $city_text 市文本
 * @property int $district_id 区ID
 * @property string $district_text 区文本
 * @property string $address 详细地址
 * @property string $score 评分
 * @property string $lng 经度
 * @property string $lat 维度
 * @property string $contact_tel 联系电话
 * @property string $desc 描述
 * @property int $is_delete 是否删除：1是 0否
 * @property \Illuminate\Support\Carbon $created_at
 * @property \Illuminate\Support\Carbon $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel apiBaseQuery($whereParams = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel bgBaseQuery($whereParams = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel paramsQuery($fields, $params = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel query()
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereCityText($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereContactTel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereDistrictId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereDistrictText($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereIds($ids = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereIsDelete($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereLat($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereLng($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereProvinceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereProvinceText($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereScore($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereThumbnail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Hotel whereUpdatedAt($value)
 * @property-read \Illuminate\Database\Eloquent\Collection<int, Label> $facilities
 * @property-read int|null $facilities_count
 * @property-read mixed $faddress
 * @mixin \Eloquent
 */
class Hotel extends BaseModel
{
    /**
     * Define table name
     *
     * @var string
     */
    protected $table = 'hotels';

    /**
     * Define a primary key
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * A property that cannot be batched.
     *
     * @var array
     */
    protected $guarded = ['id'];
    protected $hidden  = ['updated_at'];
    protected $casts   = [
        'score' => 'float'
    ];

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        self::saved(function ($model) {
            /** @var self $model */
            if ($model->isDirty('province_id') || $model->isDirty('city_id') || $model->isDirty('district_id') || $model->isDirty('address')) {
                self::storeLocation($model);
            }

            if (
                $model->isDirty('name') ||
                $model->isDirty('province_id') ||
                $model->isDirty('city_id') ||
                $model->isDirty('district_id') ||
                $model->isDirty('address')
            ) {
                self::storeKeyword($model, [
                   $model->name, $model->faddress
                ]);
            }
        });
    }
    /*********************************************属性格式化***************************************************************/

    /*********************************************设置属性***************************************************************/

    /*********************************************模型操作***************************************************************/
    public function scopeBgBaseQuery($query, $whereParams = [])
    {
        $query->paramsQuery(['name'], $whereParams);
    }

    public function scopeApiBaseQuery($query, $whereParams = [])
    {
    }

    public static function getForms($fields = [], $type = 'search', $width = '300px')
    {
        $facilities = Label::hotelFacilityQuery()->select(['id as value', 'name as label'])->get();

        $forms = [
            'name'         => ['title' => '名称', 'type' => 'text'],
            'thumbnail'    => ['title' => '封面', 'type' => 'image'],
            'area_ids'     => ['title' => '区域', 'type' => 'treeSelect', 'options' => Area::getTree()],
            'address'      => ['title' => '详细地址', 'type' => 'text'],
            'score'        => ['title' => '评分', 'type' => 'number'],
            'contact_tel'  => ['title' => '联系电话', 'type' => 'text'],
            'facility_ids' => ['title' => '酒店设施', 'type' => 'multiSelect', 'options' => $facilities],
            'desc'         => ['title' => '描述', 'type' => 'textarea']
        ];

        return self::handleForms($forms, $fields, $type, $width);
    }
    /*********************************************模型关联***************************************************************/
    public function facilities()
    {
        return $this->hasManyThrough(Label::class, HotelFacility::class, 'hotel_id', 'id', 'id', 'facility_id');
    }
}
